package com.eleadmin.store.controller;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.store.entity.Customer;
import com.eleadmin.store.entity.Warehouse;
import com.eleadmin.store.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;


/**
 *
 * 客户管理
 * @author : Doman
 * @date : 2024-02-10 22:23:45
 * @version ：1.0
 */

@Api(tags = "客户管理")
@RestController
@RequestMapping("/api/store/customer")
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    @PreAuthorize("hasAuthority('store:customer:list')")
    //@ApiOperation("分页查询客户管理")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, Customer customer) throws Exception{
        return customerService.list(page, customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:customer:list')")
    //@ApiOperation("查询全部客户管理")
    @GetMapping()
    public JsonResult list(Customer customer){
        return customerService.list(customer, getLoginUser());
    }


    @PreAuthorize("hasAuthority('store:customer:list')")
    //@ApiOperation("查询全部客户管理")
    @GetMapping("setPinyin")
    public JsonResult setPinyin(Customer customer){
        return customerService.setPinyin(customer, getLoginUser());
    }


    @PreAuthorize("hasAuthority('store:customer:list')")
//    @ApiOperation("返回客户select列表")
    @GetMapping("listSel")
    public JsonResult listSel(Customer customer){
        return customerService.listSel(customer, getLoginUser());
    }


    @PreAuthorize("hasAuthority('store:customer:list')")
    //@ApiOperation("根据id查询客户管理")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return customerService.getById(id);
    }


    @PreAuthorize("hasAuthority('store:customer:save')")
    @OperationLog
    @ApiOperation(value ="添加客户管理")
    @PostMapping()
    public JsonResult save(@RequestBody Customer customer) {
        try {
            return customerService.save(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('store:customer:update')")
    @OperationLog
    @ApiOperation(value ="修改客户管理")
    @PutMapping()
    public JsonResult update(@RequestBody Customer customer) {
        try {
            return customerService.update(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:customer:remove')")
    @OperationLog
    @ApiOperation(value ="删除客户管理")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return customerService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}
